import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../model/model.dart';
import '../tool/eq_tool.dart';
import '../tool/eq_util.dart';

class ChartView extends LineCharView {
  ChannelModel? ch;
  Size size;
  ChartView(
      {required this.ch,
      required this.size,
      required super.padding,
      required super.minY,
      required super.maxY,
      super.backgroudColor = Colors.transparent});

  ///X轴的设置
  @override
  AxisLine axisX_line() {
    return AxisLine(lineColor: Colors.black, lineWidth: 0.3);
  }

  ///Y轴的设置
  @override
  AxisLine axisY_line() {
    return AxisLine(lineColor: Colors.black, lineWidth: 0.3);
  }

  ///边框的设置
  @override
  AxisLine border_line() {
    return AxisLine(lineColor: Colors.black, lineWidth: 0.5);
  }

  ///折线图的设置
  @override
  AxisLine chart_line() {
    return AxisLine(lineColor: ch == null ? Colors.black : Color(ch!.hex_line), lineWidth: 1);
  }

  ///X轴标签的文本风格设置
  @override
  TextStyle axisX_label_textStyle() {
    return const TextStyle(fontSize: 9, color: Colors.black);
  }

  ///Y轴标签的文本风格设置
  @override
  TextStyle axisY_label_textStyle() {
    return const TextStyle(fontSize: 10, color: Colors.black);
  }

  ///标签的偏移
  @override
  Offset labelOffset() {
    return const Offset(8, 5);
  }

  ///X轴的标签内容
  @override
  String? axisX_label(double minY, double maxY, double currentX, Size paintSize) {
    ///固定频率点，转x轴上的点
    var _freqsForX = [20, 100, 320, 500, 750, 1250, 3000, 6000, 10000, 14000, 22000].map(($0) {
      return EQUtil.toInt($0.toDouble(), paintSize.width).roundToDouble();
    }).toList();
    if (_freqsForX[0] == currentX) {
      return '20';
    } else if (_freqsForX[1] == currentX) {
      return '100';
    } else if (_freqsForX[2] == currentX) {
      return '320';
    } else if (_freqsForX[3] == currentX) {
      return '500';
    } else if (_freqsForX[4] == currentX) {
      return '750';
    } else if (_freqsForX[5] == currentX) {
      return '1.25k';
    } else if (_freqsForX[6] == currentX) {
      return '3k';
    } else if (_freqsForX[7] == currentX) {
      return '6k';
    } else if (_freqsForX[8] == currentX) {
      return '10k';
    } else if (_freqsForX[9] == currentX) {
      return '14k';
    } else if (_freqsForX[10] == currentX) {
      return '22k';
    }
    return null;
  }

  ///Y轴的标签内容
  @override
  String? axisY_label(double minY, double maxY, double currentY, Size paintSize) {
    if (currentY == 0.0) {
      return '20';
    } else if (currentY == (maxY / 4).round()) {
      return '10';
    } else if (currentY == (maxY / 2).round()) {
      return '0dB';
    } else if (currentY == (maxY * 3 / 4).round()) {
      return '-10';
    } else if (currentY == maxY.round()) {
      return '-20';
    }
    return null;
  }

  ///X轴方向上，Y轴的值
  @override
  List<double> axisY_updateValue(Size size) {
    var sign_list = ch!.sign_list.map(($0) => CHSign(Q: $0.Q, gain: $0.gain, freq: $0.freq)).toList();
    var high =
        CHPass(freq: ch!.high_pass!.freq, oct_type: ch!.high_pass!.type!.oct_type, isHigh: true, isByPass: false);
    var low = CHPass(freq: ch!.low_pass!.freq, oct_type: ch!.low_pass!.type!.oct_type, isHigh: false, isByPass: false);
    return EqTool.eqTotal(sign_list, high, low, size, 12, 0.5555556 * 25);
  }
}
